home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / Kibitz / Setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-06  |  7.6 KB  |  285 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        setup.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __GWLAYERS__
  21. #include <GWLayers.h>
  22. #endif
  23.  
  24. #ifndef __RESOURCES__
  25. #include <Resources.h>
  26. #endif
  27.  
  28. #ifndef __TOOLUTILS__
  29. #include <ToolUtils.h>
  30. #endif
  31.  
  32.  
  33. extern Cursor    *gCurrentCursor;
  34.  
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39. /*****************************************************************************/
  40.  
  41.  
  42.  
  43. #pragma segment Config
  44. void    DoArrangeBoard(FileRecHndl frHndl, EventRecord *event, Point clickLoc)
  45. {
  46.     short            piece, fromSq, fromRow, fromCol, toRow, toCol, toSq;
  47.     short            palettePiece, color, delta, middle, i;
  48.     Boolean            squareWasEmpty, changedPalettePiece;
  49.     Rect            paletteRect, boardRect, fromRect;
  50.     Point            releaseLoc;
  51.     ControlHandle    ctl;
  52.  
  53.     paletteRect = PaletteRect();
  54.     if (PtInRect(clickLoc, &paletteRect)) {
  55.         palettePiece = 1 + (clickLoc.h - paletteRect.left) / kBoardSqSize;
  56.         if (clickLoc.v < paletteRect.top + kBoardSqSize)
  57.             palettePiece = -palettePiece;
  58.         (*frHndl)->doc.palettePiece = palettePiece;
  59.         DrawPalette(frHndl);
  60.         SetOrigin(0, 0);
  61.         return;
  62.     }
  63.  
  64.     SetOrigin(0, 0);
  65.     clickLoc.h += 4096;
  66.  
  67.     squareWasEmpty = false;
  68.  
  69.     boardRect = BoardRect();
  70.     if (PtInRect(clickLoc, &boardRect)) {
  71.  
  72.         fromRow = (clickLoc.v - kBoardVOffset) / kBoardSqSize;
  73.         fromCol = (clickLoc.h - kBoardHOffset) / kBoardSqSize;
  74.         fromRect.top    = 1 + fromRow * kBoardSqSize;
  75.         fromRect.left   = 1 + fromCol * kBoardSqSize;
  76.         fromRect.bottom = fromRect.top  + 32;
  77.         fromRect.right  = fromRect.left + 32;
  78.  
  79.         if ((*frHndl)->doc.invertBoard) {
  80.             fromRow = 7 - fromRow;
  81.             fromCol = 7 - fromCol;
  82.         }
  83.         fromSq = START_IBNDS + 10 * fromRow + fromCol;
  84.  
  85.         piece = (*frHndl)->doc.palettePiece;
  86.         changedPalettePiece = false;
  87.         if (event->modifiers & optionKey) {
  88.             changedPalettePiece = true;
  89.             i = (*frHndl)->doc.theBoard[fromSq];
  90.             if ((i != WK) && (i != BK) && (i != EMPTY)) {
  91.                 if (i < WK) i += KSIDEPIECE;
  92.                 if (i > BK) i -= KSIDEPIECE;
  93.                 piece = (*frHndl)->doc.palettePiece = i;
  94.                 SetOrigin((kArrangeBoard * 4096), 0);
  95.                 DrawPalette(frHndl);
  96.                 SetOrigin(0, 0);
  97.             }
  98.         }
  99.  
  100.         if ((*frHndl)->doc.theBoard[fromSq] == EMPTY) {
  101.             squareWasEmpty = true;
  102.             if (piece == WP) {
  103.                 if (fromRow == 7) return;
  104.                 if (fromRow == 0) piece = WQ;
  105.             }
  106.             if (piece == BP) {
  107.                 if (fromRow == 0) return;
  108.                 if (fromRow == 7) piece = BQ;
  109.             }
  110.             (*frHndl)->doc.theBoard[fromSq]  = piece;
  111.             (*frHndl)->doc.gameIndex      = 0;
  112.             (*frHndl)->doc.numGameMoves   = 0;
  113.             (*frHndl)->fileState.docDirty = true;
  114.             ctl = (*frHndl)->doc.gameSlider;
  115.             (*ctl)->contrlMax   = 0;
  116.             (*ctl)->contrlValue = 0;
  117.  
  118.             if (fromSq == 21) (*frHndl)->doc.king[BLACK].rookMoves[QSIDE] = 0;
  119.             if (fromSq == 28) (*frHndl)->doc.king[BLACK].rookMoves[KSIDE] = 0;
  120.             if (fromSq == 91) (*frHndl)->doc.king[WHITE].rookMoves[QSIDE] = 0;
  121.             if (fromSq == 98) (*frHndl)->doc.king[WHITE].rookMoves[KSIDE] = 0;
  122.                 /* Adjust castling privileges. */
  123.  
  124.             (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  125.             (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  126.                 /* Double-pawn-push must be last change to enable en-passant. */
  127.         }
  128.  
  129.         SetCursor(*GetCursor(closedHandCursor));
  130.         gCurrentCursor = nil;
  131.  
  132.         piece = (*frHndl)->doc.theBoard[fromSq];
  133.         releaseLoc.h = releaseLoc.v = 0x4000;
  134.         if (piece) MoveThePiece(frHndl, fromSq, fromRect, clickLoc, &releaseLoc);
  135.  
  136.         if ((toRow = releaseLoc.v - kBoardVOffset) < 0) toRow = -1;
  137.         else                                            toRow /= kBoardSqSize;
  138.         if ((toCol = releaseLoc.h - kBoardHOffset) < 0) toCol = -1;
  139.         else                                            toCol /= kBoardSqSize;
  140.         if ((*frHndl)->doc.invertBoard) {
  141.             toRow = 7 - toRow;
  142.             toCol = 7 - toCol;
  143.         }
  144.         if ((toRow > -1) && (toRow < 8) && (toCol > -1) && (toCol < 8)) {
  145.             toSq = START_IBNDS + 10 * toRow + toCol;
  146.             if (toSq == fromSq) {
  147.                 if (!squareWasEmpty) {
  148.                     if (!changedPalettePiece) {
  149.                         if ((piece != WK) && (piece != BK)) {
  150.                             (*frHndl)->doc.theBoard[fromSq] = EMPTY;
  151.                             SetOrigin((kArrangeBoard * 4096), 0);
  152.                             DrawPalette(frHndl);
  153.                             SetOrigin(0, 0);
  154.                         }
  155.                     }
  156.                 }
  157.                 ImageDocument(frHndl, true);
  158.                 return;
  159.             }
  160.         }
  161.         else toSq = fromSq;
  162.  
  163.         if (((*frHndl)->doc.theBoard[toSq] == BK) || ((*frHndl)->doc.theBoard[toSq] == WK)) {
  164.             ImageDocument(frHndl, true);
  165.             return;
  166.         }
  167.  
  168.         piece = (*frHndl)->doc.theBoard[fromSq];
  169.         if ((piece == WP) && (toSq >= 91)) {
  170.             ImageDocument(frHndl, true);
  171.             return;
  172.         }
  173.         if ((piece == WP) && (toSq <= 28)) piece = WQ;
  174.         if ((piece == BP) && (toSq <= 28)) {
  175.             ImageDocument(frHndl, true);
  176.             return;
  177.         }
  178.         if ((piece == BP) && (toSq >= 91)) piece = BQ;
  179.  
  180.         (*frHndl)->doc.theBoard[toSq]    = piece;
  181.         (*frHndl)->doc.theBoard[fromSq]  = EMPTY;
  182.         (*frHndl)->doc.gameIndex      = 0;
  183.         (*frHndl)->doc.numGameMoves   = 0;
  184.         (*frHndl)->fileState.docDirty = true;
  185.         ctl = (*frHndl)->doc.gameSlider;
  186.         (*ctl)->contrlMax   = 0;
  187.         (*ctl)->contrlValue = 0;
  188.  
  189.         if ((piece == WK) || (piece == BK)) {
  190.             color = (piece < 0) ? WHITE : BLACK;
  191.             (*frHndl)->doc.king[color].kingLoc   = toSq;
  192.             (*frHndl)->doc.king[color].kingMoves = true;
  193.         }
  194.  
  195.         if ((fromSq==21) || (toSq==21)) ++(*frHndl)->doc.king[BLACK].rookMoves[QSIDE];
  196.         if ((fromSq==28) || (toSq==28)) ++(*frHndl)->doc.king[BLACK].rookMoves[KSIDE];
  197.         if ((fromSq==91) || (toSq==91)) ++(*frHndl)->doc.king[WHITE].rookMoves[QSIDE];
  198.         if ((fromSq==98) || (toSq==98)) ++(*frHndl)->doc.king[WHITE].rookMoves[KSIDE];
  199.             /* Adjust castling privileges. */
  200.  
  201.         (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  202.         (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  203.             /* Double-pawn-push must be last change to enable en-passant. */
  204.  
  205.         delta = 0;
  206.         if (piece == WP)
  207.             if ((fromSq >= 81) && (fromSq <= 88))
  208.                 delta = fromSq - toSq;
  209.         if (piece == BP)
  210.             if ((fromSq >= 31) && (fromSq <= 38))
  211.                 delta = toSq - fromSq;
  212.         if (delta == 20) {
  213.             middle = (fromSq + toSq) / 2;
  214.             if (!(*frHndl)->doc.theBoard[middle]) {
  215.                 (*frHndl)->doc.enPasMove        = middle;
  216.                 (*frHndl)->doc.arngEnPasMove    = middle;
  217.                 (*frHndl)->doc.enPasPawnLoc     = toSq;
  218.                 (*frHndl)->doc.arngEnPasPawnLoc = toSq;
  219.             }
  220.         }
  221.  
  222.         ImageDocument(frHndl, true);
  223.     }
  224. }
  225.  
  226.  
  227.  
  228. /*****************************************************************************/
  229.  
  230.  
  231.  
  232. #pragma segment Config
  233. void    DrawPalette(FileRecHndl frHndl)
  234. {
  235.     GrafPtr        curPort;
  236.     short        rr, cc, piece;
  237.     Rect        paletteRect;
  238.     CIconHandle    pieceIcon;
  239.  
  240.     GetPort(&curPort);
  241.     if (curPort->portRect.left != -4096) return;
  242.  
  243.     PenSize(1, 1);
  244.     for (rr = 1; rr < 6; ++rr) {
  245.         for (cc = -1; cc < 3; cc += 2) {
  246.             paletteRect = PaletteRect();
  247.             paletteRect.left += (rr - 1) * kBoardSqSize;
  248.             if (cc == 1) paletteRect.top += kBoardSqSize;
  249.             paletteRect.right  = paletteRect.left + 32;
  250.             paletteRect.bottom = paletteRect.top + 32;
  251.             piece = rr * cc;
  252.             if (piece == (*frHndl)->doc.palettePiece) piece += 13;
  253.             pieceIcon = ReadCIcon(263 + piece);
  254.             DrawCIconNoMask(pieceIcon, paletteRect);
  255.             InsetRect(&paletteRect, -1, -1);
  256.             FrameRect(&paletteRect);
  257.             KillCIcon(pieceIcon);
  258.         }
  259.     }
  260.     PenNormal();
  261. }
  262.  
  263.  
  264.  
  265. /*****************************************************************************/
  266.  
  267.  
  268.  
  269. #pragma segment Config
  270. Rect    PaletteRect(void)
  271. {
  272.     Rect    paletteRect;
  273.  
  274.     paletteRect.top    = 74;
  275.     paletteRect.left   = kBoardWidth + 21;
  276.     paletteRect.right  = paletteRect.left + 5 * kBoardSqSize;
  277.     paletteRect.bottom = paletteRect.top + 2 * kBoardSqSize;
  278.     OffsetRect(&paletteRect, -4096, 0);
  279.  
  280.     return(paletteRect);
  281. }
  282.  
  283.  
  284.  
  285.